package com.gw.quotes.activity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gw.quotes.R;
import com.gw.quotes.adapter.QuoteAccquotationAdapter;
import com.gw.quotes.adapter.QuoteProjectaccessorieAdapter;
import com.gw.quotes.entity.business.ProjectInfoEntity.ProjectInfo;
import com.gw.quotes.entity.business.ProjectInfoEntity.ProjectInfo.Projectquotation;
import com.gw.quotes.entity.business.ProjectListEntity.ProjectItem;
import com.gw.quotes.net.BaseServiceInter;
import com.gw.quotes.net.HttpCallBack;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;
import com.gw.quotes.net.service.GetCurrTimeImpl;
import com.gw.quotes.net.service.ProjectInfoQueryServiceImpl;
import com.gw.quotes.util.CommonTools;
import com.yxw.view.NoScrollListView;

public class QuoteDetailActivity extends BaseActivity {

	private QuoteProjectaccessorieAdapter projectaccessorieAdapter;
	private QuoteAccquotationAdapter accquotationAdapter;
	// private List<Accquotation> accquotationList;
	// private List<Projectaccessorie> projectaccessorieList;

	private ProjectItem projectItem;
	private ProjectInfo projectInfo;

	@ViewInject(id = R.id.back_img, click = "back")
	private ImageView back_img;
	@ViewInject(id = R.id.title)
	private TextView title;
	@ViewInject(id = R.id.end_time_text)
	private TextView end_time;
	@ViewInject(id = R.id.pro_no_text)
	private TextView pro_no;
	@ViewInject(id = R.id.purchase_unit_text)
	private TextView purchase_unit;
	@ViewInject(id = R.id.join_supplier_text)
	private TextView join_supplier;
	@ViewInject(id = R.id.quote_office_listview)
	private NoScrollListView quote_office_listview;
	@ViewInject(id = R.id.quote_info_listview)
	private NoScrollListView quote_info_listview;
	@ViewInject(id = R.id.goods)
	private TextView goods;
	@ViewInject(id = R.id.number)
	private TextView number;
	@ViewInject(id = R.id.unit_price)
	private TextView unit_price;
	@ViewInject(id = R.id.operation)
	private TextView operation;
	@ViewInject(id = R.id.quote_btn, click = "onQuote")
	private ImageView quote_btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_quote_detail);
		getIntentData();
		initView();
		requestInfo();
	}

	private void getIntentData() {
		Intent intent = getIntent();
		projectItem = (ProjectItem) intent.getSerializableExtra("projectItem");
	}

	private void initView() {
		// LayoutParams para = quote_btn.getLayoutParams();
		// para.height = projectaccessorieList.size() * (AppInfo.SCREEN_WIDTH /
		// 9)
		// + 2;
		// para.width = AppInfo.SCREEN_WIDTH / 5;
		// quote_btn.setLayoutParams(para);
		title.setText(projectItem.getGoodsclassname());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		end_time.setText(df.format(projectItem.getEndtime()));
		pro_no.setText(projectItem.getProjectcode());
		purchase_unit.setText(projectItem.getBuyerorgname());
		join_supplier.setText(projectItem.getQuotationcount() + "");
		if (projectItem.getProjectGoodList() != null
				&& projectItem.getProjectGoodList().size() > 0
				&& projectItem.getProjectGoodList().get(0) != null) {
			title.setText(projectItem.getProjectGoodList().get(0)
					.getGoodsname());
			projectaccessorieAdapter = new QuoteProjectaccessorieAdapter(this,
					R.layout.item_quote_detailed, projectItem);
			quote_office_listview.setAdapter(projectaccessorieAdapter);

		}
	}

	private void requestInfo() {
		dialog.show();
		BaseServiceInter httpService = new ProjectInfoQueryServiceImpl();
		Request request = new Request();
		request.setProjectguid(projectItem.getProjectguid());
		request.setProjecttype(projectItem.getProjecttype());
		request.setLoginname(getLoginUser().getLoginname());
		request.setTicket(getLoginUser().getTicket());
		Response response = httpService.handler(request,
				new HttpCallBack<Response<ProjectInfo>>() {
					@Override
					public void onFinished(Response<ProjectInfo> response) {
						if (response != null) {
							if (response.getResult() == response.RESULT_SUCCESS) {
								projectInfo = response.getObj();
								getCurTiem();
								// 更新UI
								updateUI();
							} else {
								CommonTools.showShortToast(
										QuoteDetailActivity.this,
										response.getMsg());
								QuoteDetailActivity.this.finish();
								dialog.dismiss();
							}
						} else {
							CommonTools.showShortToast(
									QuoteDetailActivity.this, "信息获取失败！");
							QuoteDetailActivity.this.finish();
							dialog.dismiss();
						}
					}
				});
	}

	/**
	 * 得到获取网络时间时的手机当前时间 用于计算时间偏移量
	 */
	private long cur_sys_time;
	/**
	 * 获取网络当前时间
	 */
	private long cur_net_time;

	/** 上次总报价 */
	private double totalPricLast = 0;

	/**
	 * 获取网络时间
	 */
	private void getCurTiem() {
		BaseServiceInter httpService = new GetCurrTimeImpl();
		Request request = new Request();
		Response response = httpService.handler(request,
				new HttpCallBack<Response>() {
					@Override
					public void onFinished(Response response) {
						if (response != null
								&& response.getResult() == Response.RESULT_SUCCESS) {
							cur_sys_time = new Date().getTime();
							cur_net_time = Long.parseLong(response.getObj()
									.toString());
							Log.e("yxw", cur_sys_time + " " + cur_net_time);
						} else {
							CommonTools.showShortToast(
									QuoteDetailActivity.this, "获取时间失败！");
						}
						dialog.dismiss();
					}
				});

	}

	private void updateUI() {
		if (projectInfo != null
				&& projectInfo.getProjectquotationList() != null
				&& projectInfo.getProjectquotationList().size() > 0) {

			List<Projectquotation> projectquotations = projectInfo
					.getProjectquotationList();
			accquotationAdapter = new QuoteAccquotationAdapter(this,
					R.layout.item_quote_info, projectquotations);
			quote_info_listview.setAdapter(accquotationAdapter);

			for (Projectquotation projectquotation : projectquotations) {
				if (totalPricLast == 0) {
					totalPricLast = projectquotation.getTotalpric();
				} else if (totalPricLast > projectquotation.getTotalpric()) {
					totalPricLast = projectquotation.getTotalpric();
				}
			}

			/**
			 * 这部分数据重复，没必要重复更新UI
			 */
			// if (projectItem == null || projectItem.getProjectGoodList() ==
			// null
			// || projectItem.getProjectGoodList().size() == 0
			// || projectItem.getProjectGoodList().get(0) != null) {
			//
			// if (projectInfo.getProjectGoodList() != null
			// && projectInfo.getProjectGoodList().size() > 0
			// && projectInfo.getProjectGoodList().get(0) != null) {
			// title.setText(projectInfo.getGoodsclassname());
			// end_time.setText(DateFormat.format("yyyy-MM-dd hh:mm:ss",
			// projectInfo.getEndtime()));
			// pro_no.setText(projectInfo.getProjectcode());
			// purchase_unit.setText(projectInfo.getBuyerorgname());
			// join_supplier.setText(projectInfo.getQuotationcount() + "");
			// projectaccessorieList = projectInfo.getProjectGoodList()
			// .get(0).getProjectaccessorieList();
			// title.setText(projectItem.getProjectGoodList().get(0)
			// .getGoodsname());
			// if (projectaccessorieList == null) {
			// projectaccessorieList = new ArrayList<Projectaccessorie>();
			// }
			// projectaccessorieAdapter = new QuoteProjectaccessorieAdapter(
			// this, R.layout.item_quote_detailed,
			// projectaccessorieList);
			// quote_office_listview.setAdapter(projectaccessorieAdapter);
			// }
			//
			// }
		}
	}

	public void onQuote(View v) {
		// 判断当前网络时间是否超过截至时间
		if (projectItem != null) {
			if (projectItem.getEndtime().getTime() > cur_net_time
					+ (new Date().getTime() - cur_sys_time)) {
				Intent intent = new Intent(this,
						QuoteDetailQuotingActivity.class);
				intent.putExtra("projectInfo", projectInfo);
				intent.putExtra("end_time", projectItem.getEndtime().getTime());
				intent.putExtra("cur_net_time", cur_net_time);
				intent.putExtra("cur_sys_time", cur_sys_time);
				intent.putExtra("cur_sys_time", cur_sys_time);
				intent.putExtra("totalPric_last", totalPricLast);

				startActivityForResult(intent, 1);
			} else {
				CommonTools.showShortToast(QuoteDetailActivity.this,
						"抱歉，已超过报价截止时间！");
			}

		} else {
			CommonTools.showShortToast(QuoteDetailActivity.this, "未知错误，请重启应用！");
		}

	}

	public void back(View v) {
		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1 && resultCode == 2) {
			requestInfo();
		}
	}
}
